﻿using System;
using fastCSharp;
using System.Threading;

namespace diantou.web
{
    public static class sqlClientCache
    {
        /// <summary>
        /// 用户客户端缓存
        /// </summary>
        public static readonly fastCSharp.sql.logStream<diantou.dataAccess.user, diantou.dataModel.user>.identityClient User;
        /// <summary>
        /// 题目分组客户端缓存
        /// </summary>
        public static readonly fastCSharp.sql.logStream<diantou.dataAccess.problemGroup, diantou.dataModel.problemGroup>.identityClient ProblemGroup;
        /// <summary>
        /// 题目客户端缓存
        /// </summary>
        public static readonly fastCSharp.sql.logStream<diantou.dataAccess.problem, diantou.dataModel.problem>.identityClient Problem;
        /// <summary>
        /// 企业考试客户端缓存
        /// </summary>
        public static readonly fastCSharp.sql.logStream<diantou.dataAccess.exam, diantou.dataModel.exam>.identityClient Exam;
        /// <summary>
        /// 比赛客户端缓存
        /// </summary>
        public static readonly fastCSharp.sql.logStream<diantou.dataAccess.contest, diantou.dataModel.contest>.identityClient Contest;
        /// <summary>
        /// 问题客户端缓存
        /// </summary>
        public static readonly fastCSharp.sql.logStream<diantou.dataAccess.question, diantou.dataModel.question>.identityClient Question;
        /// <summary>
        /// 教程客户端缓存
        /// </summary>
        public static readonly fastCSharp.sql.logStream<diantou.dataAccess.tutorial, diantou.dataModel.tutorial>.identityClient Tutorial;
        /// <summary>
        /// 收藏客户端缓存
        /// </summary>
        public static readonly fastCSharp.sql.logStream<diantou.dataAccess.favorite, diantou.dataModel.favorite>.identityClient Favorite;
        /// <summary>
        /// 勋章客户端缓存
        /// </summary>
        public static readonly fastCSharp.sql.logStream<diantou.dataAccess.medal, diantou.dataModel.medal>.identityClient Medal;
        /// <summary>
        /// 日志流客户端集合
        /// </summary>
        private static subArray<fastCSharp.sql.logStream.client> clients;
        /// <summary>
        /// 保存缓存访问锁
        /// </summary>
        private static int saveLock;
        /// <summary>
        /// 保存缓存
        /// </summary>
        public static void Save()
        {
            if (Interlocked.CompareExchange(ref saveLock, 0, 1) == 0)
            {
                try
                {
                    foreach (fastCSharp.sql.logStream.client client in clients) client.Save();
                }
                finally { saveLock = 0; }
            }
        }
        static sqlClientCache()
        {
            string cachePath = fastCSharp.config.pub.Default.CachePath + @"sqlStreamCache\";
#if FASTCSHARPCODE
            clients.Add(User = new fastCSharp.sql.logStream<dataAccess.user, dataModel.user>.identityClient(diantou.dataAccess.tcpCall.user.getSqlCache, diantou.dataAccess.tcpCall.user.onSqlLog, value => value.Id, diantou.dataAccess.tcpCall.user.Get, cachePath));
            clients.Add(ProblemGroup = new fastCSharp.sql.logStream<dataAccess.problemGroup, dataModel.problemGroup>.identityClient(diantou.dataAccess.tcpCall.problemGroup.getSqlCache, diantou.dataAccess.tcpCall.problemGroup.onSqlLog, value => value.Id, diantou.dataAccess.tcpCall.problemGroup.Get, cachePath, 16));
            clients.Add(Problem = new fastCSharp.sql.logStream<dataAccess.problem, dataModel.problem>.identityClient(diantou.dataAccess.tcpCall.problem.getSqlCache, diantou.dataAccess.tcpCall.problem.onSqlLog, value => value.Id, diantou.dataAccess.tcpCall.problem.Get, cachePath, 128));
            clients.Add(Exam = new fastCSharp.sql.logStream<dataAccess.exam, dataModel.exam>.identityClient(diantou.dataAccess.tcpCall.exam.getSqlCache, diantou.dataAccess.tcpCall.exam.onSqlLog, value => value.Id, diantou.dataAccess.tcpCall.exam.Get, cachePath, 16));
            clients.Add(Contest = new fastCSharp.sql.logStream<dataAccess.contest, dataModel.contest>.identityClient(diantou.dataAccess.tcpCall.contest.getSqlCache, diantou.dataAccess.tcpCall.contest.onSqlLog, value => value.Id, diantou.dataAccess.tcpCall.contest.Get, cachePath, 16));
            clients.Add(Question = new fastCSharp.sql.logStream<dataAccess.question, dataModel.question>.identityClient(diantou.dataAccess.tcpCall.question.getSqlCache, diantou.dataAccess.tcpCall.question.onSqlLog, value => value.Id, diantou.dataAccess.tcpCall.question.Get, cachePath));
            clients.Add(Tutorial = new fastCSharp.sql.logStream<dataAccess.tutorial, dataModel.tutorial>.identityClient(diantou.dataAccess.tcpCall.tutorial.getSqlCache, diantou.dataAccess.tcpCall.tutorial.onSqlLog, value => value.Id, diantou.dataAccess.tcpCall.tutorial.Get, cachePath, 16));
            clients.Add(Favorite = new fastCSharp.sql.logStream<dataAccess.favorite, dataModel.favorite>.identityClient(diantou.dataAccess.tcpCall.favorite.getSqlCache, diantou.dataAccess.tcpCall.favorite.onSqlLog, value => value.Id, diantou.dataAccess.tcpCall.favorite.Get, cachePath));
            clients.Add(Medal = new fastCSharp.sql.logStream<dataAccess.medal, dataModel.medal>.identityClient(diantou.dataAccess.tcpCall.medal.getSqlCache, diantou.dataAccess.tcpCall.medal.onSqlLog, value => value.Id, diantou.dataAccess.tcpCall.medal.Get, cachePath, 16));
#endif
        }
    }
}
